package net.zjitc.dao.impl;

import net.zjitc.dao.OrderDao;
import net.zjitc.pojo.Order;
import net.zjitc.pojo.Orderitem;
import net.zjitc.utils.db.C3P0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by Administrator on 2017/12/13.
 */
public class OrderDaoImpl implements OrderDao{

    QueryRunner runner=new QueryRunner();//数据库连接对象，一定是事务开启时连接的对象，所以不需数据库连接源自动配给

    public int add(Order order) {
        String sql="insert into orders values(?,?,?,?,?,?,?,?)";
        Connection conn=null;
        try {
            conn=C3P0Utils.getConnection();
            //1.插入订单
            Object[] obj={order.getOid(),order.getTotal(),order.getOrdertime(),order.getState(),order.getName(),
            order.getPhone(),order.getAddr(),order.getUser().getUid()};//把多个参数封成一个数组

            int count=runner.update(conn,sql,obj);

            /*
            * 订单子项是多条记录，可使用循环语句，一条条插入
            * 批量处理方法
            * 使用循环，从订单子项的集合中取出每条记录，然后把相应的参数封装在相应的数组中，组成一个二维数组
            * */

            //2.插入订单子项
            sql="insert into orderitem(count,subtotal,pid,oid,osatate) values(?,?,?,?,?)";

            List<Orderitem> list=order.getOrderitemList();//订单子项的集合
            int len=list.size();

            Object[][] objs=new Object[len][];

            for (int i=0;i<len;i++){
                Orderitem item=list.get(i);//从集合中取出第i条记录
                objs[i]=new Object[]{item.getCount(),item.getSubtotal(),item.getProduct().getPid(),
                item.getOrder().getOid(),item.getOsatate()};
            }
            //批量处理
            runner.batch(conn,sql,objs);
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public List<Order> findByUid(int uid) {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="select * from orders where uid=?";
        try {
            List<Order> list= (List<Order>) runner.query(sql,new BeanListHandler(Order.class),uid);
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<Orderitem> findOrderitemByOid(String oid) {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="select * from orderitem where oid=?";
        try {
            List<Orderitem> list= (List<Orderitem>) runner.query(sql,new BeanListHandler(Orderitem.class),oid);
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public int findPidByItemid(int itemid) {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="select pid from orderitem where itemid=?";
        try {
            Number num= (Number) runner.query(sql,new ScalarHandler(),itemid);
            int pid=num.intValue();
            return pid;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public List<Order> findAll() {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="select * from orders";
        try {
            List<Order> list= (List<Order>) runner.query(sql,new BeanListHandler(Order.class));
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<Order> findbyState(int state) {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="select * from orders where state=?";
        try {
            List<Order> list= (List<Order>) runner.query(sql,new BeanListHandler(Order.class),state);
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    public int editState(String oid) {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="update orders set state=3 where oid=?";
        try {
            int count=runner.update(sql,oid);
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public int editPayState(String oid) {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="update orders set state=2 where oid=?";
        try {
            int count=runner.update(sql,oid);
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public int editStateFour(String oid) {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="update orders set state=4 where oid=?";
        try {
            int count=runner.update(sql,oid);
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public int editOstate(int itemid) {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="update orderitem set osatate=1 where itemid=?";
        try {
            int count=runner.update(sql,itemid);
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public int editOstateByItemid(String itemid) {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="update orderitem set osatate=3 where itemid=?";
        try {
            int count=runner.update(sql,itemid);
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }


}





















